<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>滴滴法律</title>
	<link rel="stylesheet" type="text/css" href="css/normalize.css">
	<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="css/style.css">
	<script type="text/javascript" src="js/jquery.min.js"></script>
	<!--[if lt IE 9]> 
		<script> src="js/html5shiv.min.js"></script> 
	<![endif]-->
</head>
<body>
	<div class="well well-sm">法律知识 / 添加文章</div>
	<form class="form-horizontal" method="post" action="" id="knowledge-form" style="margin-top:10px;">
		<div class="form-group">
			<label for="part" class="col-sm-2 control-label">分类</label>
			<div class="col-sm-2">
				<select name="bigTypeId" id="part" class="form-control">
					<option value="0">全部</option>
					<c:forEach items="${type}" var="type">
						<option value="${type.lawknowid}">${type.name}</option>
					</c:forEach>
				</select>					
			</div>
			<div class="col-sm-2">
				<select name="smallTypeId" id="small-type" class="form-control">
					<option>全部</option>
				</select>
			</div>
		</div>
		<div class="form-group">
			<label for="title" class="col-sm-2 control-label">标题</label>
			<div class="col-sm-4">
				<input id="title" type="text" name="title" class="form-control" placeholder="请填写标题">
			</div>
		</div>
		<div class="form-group">
			<label for="author" class="col-sm-2 control-label">作者</label>
			<div class="col-sm-4">
				<input id="author" type="text" name="author" class="form-control" placeholder="请填写作者">
			</div>
		</div>
		<div class="form-group">
			<label for="content" class="col-sm-2 control-label">内容</label>
			<div class="col-sm-4">
				<textarea id="content" name="content" class="form-control" placeholder="请填写文章内容" rows="4"></textarea>
			</div>
		</div>
		<div class="form-group">
			<div class="col-sm-offset-2 col-sm-4">
				<input type="button" id="add-btn" class="btn btn-success" value="添加">
			</div>
		</div>
	</form>
	
</body>

<script type="text/javascript">
 $('#part').change(function() {
 	var typeId = $('#part').val();
 	$.ajax({
 		type:'get',
 		url:'knowledge/type/' + typeId,
 		success:function(data){
 			$("#small-type option").remove();
 			$("#small-type").append("<option value='0'>全部</option>"); 
 			$.each(data.smallType,function(n,value) {
	 			$("#small-type").append("<option value='"+ value.typeid +"'>" + value.name + "</option>"); 
 			});
 		}
 	});
 });
 
 $("#add-btn").on("click",function() {
    var flag = true;
 	var title = $("#title").val();
 	var author = $("#author").val();
 	var content = $("#content").val().substring(0,5);
 	if (title == "") {
 		alert("请填写题目！");
 		flag = false;
 	}else if (author == "") {
 		alert("请填写作者！");
 		flag = false;
 	}else if (content == "") {
 		alert("请填写内容！");
 		flag = false;
 	}
 	
 	if (flag) {
 		$.ajax({
 			type:"post",
 			url:"knowledge/add",
 			data:$("#knowledge-form").serialize(),
 			success:function(data) {
 				alert(data.msg);
 				if (data.code == 200) {
 					location.href = "knowledge/index";
 				}
 			}
 		});
 	}
 
 });
</script>
</html>